<?php

session_start();

header('Access-Control-Allow-Origin:*');

date_default_timezone_set("PRC");

header("Content-Type: text/html;charset=utf8");

require_once '../common/in_mysql.php';
require_once '../common/in_mongo.php';
require_once '../db/user.db.php';

$action = filter_input(INPUT_POST, "action") ? filter_input(INPUT_POST, "action") : filter_input(INPUT_GET, "action");

$res = array();
switch ($action) {
    case 'startStat':
        $res = startStat();
        break;
    case 'activeStat':
        $res = activeStat();
        break;
    case 'userStat':
        $res = userStat();
        break;
    case 'teacherStat':
        $res = teacherStat();
        break;
    case 'delUser':
        $res = delUser();
        break;
    case 'getUser':
        $res = getUser();
        break;
    case 'SearchUser':
        $res = searchUser();
        break;
    default :
}
echo json_encode($res);

function startStat() {
    $type = filter_input(INPUT_POST, "type");
    $start = filter_input(INPUT_POST, "start");
    $end = filter_input(INPUT_POST, "end");
    $chara = filter_input(INPUT_POST, "chara");

    try {
        $end = date('Y-m-d', strtotime("$end +1 day"));
        $s = strtotime($start);
        $e = strtotime($end);
    } catch (Exception $e) {
        $res["code"] = 103;
        $res['msg'] = "日期格式错误";
        return $res;
    }

    if (intval($s) >= intval($e)) {
        $res["code"] = 102;
        $res['msg'] = "开始时间应该在结束时间之前";
        return $res;
    }
    if ((intval($e) - intval($s)) > 2592000 * 12) {
        $res["code"] = 104;
        $res['msg'] = "日期跨度不能超过1年";
        return $res;
    }

    $userDb = new UserDb();
    $array = $userDb->getStartStat($type, $s, $e, $chara);
    if ($array == "") {
        $res["code"] = 105;
        $res['msg'] = "类型不正确";
        return $res;
    }
    $data = $array['data'];
    $count = $array['count'];

    $temp = array();
    for ($i = 0; $i < $count; $i++) {
        $date = date("Y-m-d", $data[$i]['uptime']);
        if (isset($temp[$date])) {
            $temp[$date] ++;
        } else {
            $temp[$date] = 1;
        }
    }
    $data = array();
    for ($d = $s; $d < $e; $d += 86400) {
        $date = date("Y-m-d", $d);
        if (key_exists($date, $temp)) {
            $data[$date] = $temp[$date];
        } else {
            $data[$date] = 0;
        }
    }
    $res["code"] = 0;
    $res['msg'] = "";
    $res['date'] = $data;
    $res['total'] = $count;
    return $res;
}

function activeStat() {
    $type = filter_input(INPUT_POST, "type");
    $start = filter_input(INPUT_POST, "start");
    $end = filter_input(INPUT_POST, "end");
    $chara = filter_input(INPUT_POST, "chara");

    try {
        $end = date('Y-m-d', strtotime("$end +1 day"));
        $s = strtotime($start);
        $e = strtotime($end);
    } catch (Exception $e) {
        $res["code"] = 103;
        $res['msg'] = "日期格式错误";
        return $res;
    }

    if (intval($s) >= intval($e)) {
        $res["code"] = 102;
        $res['msg'] = "开始时间应该在结束时间之前";
        return $res;
    }
    if ((intval($e) - intval($s)) > 2592000 * 12) {
        $res["code"] = 104;
        $res['msg'] = "日期跨度不能超过1年";
        return $res;
    }

    $userDb = new UserDb();
    $array = $userDb->getActiveStat($type, $s, $e, $chara);
    if ($array == "") {
        $res["code"] = 105;
        $res['msg'] = "类型不正确";
        return $res;
    }
    $count = count($array);
    $temp = array();
    for ($i = 0; $i < $count; $i++) {
        $date = date("Y-m-d", $array[$i]['active_time']);
        if (isset($temp[$date])) {
            $temp[$date] ++;
        } else {
            $temp[$date] = 1;
        }
    }
    $data = array();
    for ($d = $s; $d < $e; $d += 86400) {
        $date = date("Y-m-d", $d);
        if (key_exists($date, $temp)) {
            $data[$date] = $temp[$date];
        } else {
            $data[$date] = 0;
        }
    }
    $res["code"] = 0;
    $res['msg'] = "";
    $res['date'] = $data;
    $res['total'] = $count;

    return $res;
}

function userStat() {
    $type = filter_input(INPUT_POST, "type");
    $deviceId = filter_input(INPUT_POST, "device");
    $page = filter_input(INPUT_POST, "page");
    $pageSize = filter_input(INPUT_POST, "limit");
    if ($page == "" || (!is_numeric($page))) {
        $page = 0;
    } else if ($page > 0) {
        $page--;
    }
    if ($pageSize == "" || (!is_numeric($pageSize))) {
        $pageSize = 50;
    }
    $userDb = new UserDb();
    $res["code"] = 0;
    $res['msg'] = "";
    $res['data'] = $userDb->getUserStatByPage($type, $deviceId, $page, $pageSize);
    $res['total'] = $userDb->getUserStat($type, $deviceId, $page, $pageSize);
    return $res;
}

function getUser() {
    $userDb = new UserDb();
    $array = $userDb->getUserActive();
    $active = $array['active'];
    $noActive = $array['noActive'];
    if ($array) {
        $res = $array;
        $res["code"] = 0;
        $res['msg'] = "查询成功";
        return $res;
    } else {
        $res["code"] = 105;
        $res['msg'] = "查询失败";
        return $res;
    }
}

function teacherStat() {
    $page = filter_input(INPUT_POST, "page");
    $pageSize = filter_input(INPUT_POST, "limit");

    if ($page == "" || (!is_numeric($page))) {
        $page = 0;
    } else if ($page > 0) {
        $page--;
    }
    if ($pageSize == "" || (!is_numeric($pageSize))) {
        $pageSize = 50;
    } else {
        $pageSize = intval($pageSize);
    }
    $userDb = new UserDb();
    $zoneArray = $userDb->getZone();
    $total = count($userDb->getAllTeacher());

    $zoneMap = array();
    for ($i = 0, $len = count($zoneArray); $i < $len; $i++) {
        $zoneMap[$zoneArray[$i]['zid']] = $zoneArray[$i];
    }

    $data = $userDb->getTeacherList($page, $pageSize);
    for ($i = 0, $len = count($data); $i < $len; $i++) {
        $data[$i]['index'] = $i + 1;
        if (is_numeric($data[$i]['zone'])) {
            $data[$i]['zoneName'] = $zoneMap[$data[$i]['zone']]['name'];
            $data[$i]['countryName'] = $zoneMap[$zoneMap[$data[$i]['zone']]['pzone']]['name'];
        } else {
            $data[$i]['zoneName'] = "";
            $data[$i]['countryName'] = "";
        }
        if ($data[$i]['regtime'] == null || $data[$i]['regtime'] == "" || $data[$i]['regtime'] == 0) {
            $data[$i]['regtime'] = "";
        } else {
            $data[$i]['regtime'] = date("Y-m-d H:i:s", $data[$i]['regtime']);
        }
    }

    $res["code"] = 0;
    $res['msg'] = "";
    $res['data'] = $data;
    $res['count'] = $total;

    return $res;
}

function delUser() {
    $id = filter_input(INPUT_POST, "userId");
    $userDb = new UserDb();
    if ($userDb->del_user($id)) {
        $res['code'] = 0;
        $res['msg'] = "删除成功";
    } else {
        $res['code'] = 205;
        $res['msg'] = "删除失败";
    }
    return $res;
}

function searchTeacher() {
    $id = filter_input(INPUT_POST, "userId");
    $userDb = new UserDb();
    if ($userDb->del_user($id)) {
        $res['code'] = 0;
        $res['msg'] = "删除成功";
    } else {
        $res['code'] = 205;
        $res['msg'] = "删除失败";
    }
    return $res;
}

function searchUser() {
    $keyword = filter_input(INPUT_POST, "keyword");
    $userDb = new UserDb();

    if ($keyword == "") {
        $res["code"] = 0;
        $res['msg'] = "";
        $res['data'] = array();
        $res['total'] = 0;
        $res['count'] = 0;
        return $res;
    }

    $userArray = $userDb->searchUser($keyword);

    $len = count($userArray);
    if ($len == 0) {
        $res["code"] = 0;
        $res['msg'] = "";
        $res['data'] = array();
        $res['total'] = 0;
        $res['count'] = 0;
        return $res;
    }


    $zoneArray = $userDb->getZone();
    $zoneMap = array();
    for ($i = 0, $len = count($zoneArray); $i < $len; $i++) {
        $zoneMap[$zoneArray[$i]['zid']] = $zoneArray[$i];
    }
    
    $data = $userArray;
    for ($i = 0, $len = count($data); $i < $len; $i++) {
        $data[$i]['index'] = $i + 1;
        if (is_numeric($data[$i]['zone'])) {
            $data[$i]['zoneName'] = $zoneMap[$data[$i]['zone']]['name'];
            $data[$i]['countryName'] = $zoneMap[$zoneMap[$data[$i]['zone']]['pzone']]['name'];
        } else {
            $data[$i]['zoneName'] = "";
            $data[$i]['countryName'] = "";
        }
        if ($data[$i]['regtime'] == null || $data[$i]['regtime'] == "" || $data[$i]['regtime'] == 0) {
            $data[$i]['regtime'] = "";
        } else {
            $data[$i]['regtime'] = date("Y-m-d H:i:s", $data[$i]['regtime']);
        }
    }

    $res["code"] = 0;
    $res['msg'] = "";
    $res['data'] = $data;
    $res['count'] = $len;

    return $res;

    $res["code"] = 0;
    $res['msg'] = "";
    $res['data'] = $data;
    $res['count'] = $len;
    return $res;
}
